<template>
  <div class="app-container">
    <el-row>
      <el-col :span="17">
        <el-form ref="form" :model="form" inline>
          <el-form-item prop="search">
            <el-input
              v-model="form.search"
              clearable
              style="width: 300px"
              prefix-icon="el-icon-search"
              placeholder="输入车牌号(carplate)搜索"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="success"
              icon="el-icon-search"
              size="medium"
              @click="searchByCarplate(form)"
            >搜索</el-button>
            <el-button
              type="warning"
              icon="el-icon-refresh-left"
              size="medium"
              @click="resetForm()"
            >重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>车牌名单</span>
          </div>
          <el-table
            ref="multipleTable"
            :data="tableData"
            style="width: 100%"
            highlight-current-row
          >
            <el-table-column label="车牌号码" prop="carplate">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.carplate }}</span>
              </template>
            </el-table-column>
            <el-table-column label="身份证号码" prop="userid">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.userid }}</span>
              </template>
            </el-table-column>
            <el-table-column label="用户名" prop="nickname">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.nickname }}</span>
              </template>
            </el-table-column>
            <el-table-column label="车管所名" prop="dmv">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.dmv }}</span>
              </template>
            </el-table-column>
            <el-table-column label="合格证号" prop="certificate">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.certificate }}</span>
              </template>
            </el-table-column>
            <el-table-column label="品牌种类" prop="variety">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.variety }}</span>
              </template>
            </el-table-column>
            <el-table-column label="机动车凭证" prop="voucher">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.voucher }}</span>
              </template>
            </el-table-column>
            <el-table-column label="车辆识别代号" prop="identify">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.identify }}</span>
              </template>
            </el-table-column>
            <el-table-column
              fixed="right"
              align="center"
              label="操作"
              width="220"
            >
              <template slot-scope="{ row }">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  size="mini"
                  @click="updateUser(row)"
                >编辑
                </el-button>
                <el-button

                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  @click="deleteUser(row)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <!--    调用子组件进行编辑与增加操作-->
    <cuForm
      :dialog-visible="cuDialogVisible"
      :cur-id="curId"
      @close="close"
      @search="search"
    />
  </div>
</template>
<script>
import cuForm from './components/cuForm'
import { deletePlate, getPlates, getPlatesByCarplate } from '@/api/select/plateManage'

export default {
  name: 'Roles',
  components: { cuForm },
  data() {
    return {
      form: {
        page: 1,
        size: 2,
        search: ''
      },
      tableData: [],
      multipleSelection: [],
      // cuForm数据
      cuDialogVisible: false,
      curId: ''
    }
  },
  created() {
    // 请求后端获取数据
    this.search()
  },
  methods: {
    // 获取角色列表/搜索功能
    search() {
      getPlates(this.form).then((res) => {
        this.tableData = res.data.carplates
      })
    },
    searchByCarplate() {
      getPlatesByCarplate(this.form.search).then(response => {
        this.tableData = response.data.carplates
      }).catch(error => {
        console.log(error)
      })
    },
    // 重置
    resetForm() {
      this.$refs.form.resetFields()
      this.search()
    },

    // 删除User
    deleteUser(row) {
      this.$confirm('此操作将从用户单中移除该用户, 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePlate(row.carplate).then((res) => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          // 刷新table
          this.search()
        })
      })
    },
    // 获得编辑的子窗口
    updateUser(row) {
      // 调用当前更新用户的窗口,并获取当前用户的ID
      this.curId = row.carplate
      this.cuDialogVisible = true
    },
    // 关闭子窗口
    close() {
      this.cuDialogVisible = false
      this.curId = ''
    }
  }
}
</script>
